/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;

import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Class used to show the shadow of the part when it is moved
 */
public class PseudostateDragTracker extends DragEditPartsTracker{

	/** The pseudostate editpart to move */
	protected PseudostateEditPart pseudostateEditPart = null;

	/**
	 * Creates a new PseudostateDragTracker
	 * @param portEditPart the pseudostate editpart to move
	 */
	public PseudostateDragTracker(PseudostateEditPart pseudostateEditPart){
		super(pseudostateEditPart);
		this.pseudostateEditPart = pseudostateEditPart;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.tools.DragEditPartsTracker#updateTargetRequest()
	 */
	@Override
	protected void updateTargetRequest(){
		if(pseudostateEditPart.isEntryOrExit()){
			ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
			GraphNode portg = pseudostateEditPart.getGraphNode();
			super.updateTargetRequest();
			//because of GEF we have 3 types of coordinates
			//XY absolute coordinate
			//relative to the container int he XY coodinates
			//xy coordinate in viewPort
			//get view Port
			Viewport viewport = ((FigureCanvas)((ScrollingGraphicalViewer)getCurrentViewer()).getControl()).getViewport();
			Point changementxytoXY = viewport.getViewLocation();
			//absolute position of XY
			Point portXY = LookForElement.calculateAbsolutePosition((GraphNode)(portg), new Point(0,0));
			Point mousexy = request.getLocation();
			//absolute position of the mouse
			Point mouseXY = mousexy.getCopy().translate(changementxytoXY);
			Point absoluteGoodPosition = pseudostateEditPart.inRunnerOfClass(mouseXY.getCopy());
			Point moveDelta = absoluteGoodPosition.getCopy().translate(portXY.getCopy().negate());
			request.setMoveDelta(moveDelta);
		}
		else super.updateTargetRequest();
	}
}
